#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from flask import jsonify, g

from app.libs.error_code import AuthFailed
from app.libs.redprint import RedPrint
from app.libs.token_auth import auth
from app.models.base import db
from app.models.user import User

view_api = RedPrint('user')


@view_api.route('/<int:uid>')
@auth.login_required
def user(uid):
    user = User.query.get_or_404(uid)

    return jsonify(user)


@view_api.route('/<int:uid>', methods=['GET'])
@auth.login_required
def super_get_user(uid):
    is_admin = g.user.is_admin
    if not is_admin:
        raise AuthFailed('user not admin')

    return 'success'


@view_api.route('', methods=['DELETE'])
@auth.login_required
def delete_user():
    uid = g.user.uid
    with db.auto_commit():
        user = User.query.filter_by(id=uid).first_or_404()
        user.delete()
    return 'delete ok'
